<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/11/17
 * Time: 21:38
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\admin\controller;

use app\common\business\Venue as BusObj;
use app\common\lib\Http;
use app\common\lib\Show;
use app\common\lib\Status;
use think\App;
use think\Exception;

class Venue extends Base
{
    protected BusObj $busObj;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->busObj = new BusObj();
    }

    /**
     * @return \think\response\Json
     */
    public function getVenueByList()
    {
        try {
            $venue = $this->busObj->getListData('id as value,title as name');
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        return Show::json_show(Status::status_normal, "操作成功", $venue);
    }

    public function getAddress()
    {
        $keywords = $this->request->param('keywords', '', 'trim');
        $provincesName = $this->request->param('provincesName', '', 'trim');
        if (empty($keywords)) {
            return Show::json_show(Status::status_error, "填写后");
        }
        if (empty($provincesName)) {
            $provincesName = "";
        } else {
            $provincesName = $provincesName[0]['name'];
        }
        $data = [
            "keyword" => $keywords,
            "region" => $provincesName,
            "key" => config('map.key')
        ];
        $url = "https://apis.map.qq.com/ws/place/v1/suggestion?" . http_build_query($data);
        $result = Http::get_url($url);
        if ($result['status'] == 0) {
            return Show::json_show(Status::status_normal, 'ok', $result);
        } else {
            return Show::json_show(Status::status_error, "获取失败", $result);
        }
    }
}